import CustomTable from '@/components/CustomTable/index'
import CustomUpload from '@/components/CustomUpload/index'

import {
  SaveObservationData,
  GetObservationDataList,
  DeleteObservationData,
  UploadObservationData,
  UpdateObservationData
} from '@/api/resourceManage/observation/index.js'

export default {
  components: {
    CustomTable,
    CustomUpload
  },
  data() {
    return {
      formInline: {
        demand: '',
        targetArea: '',
        sample: ''
      },
      pageCurrent: 1,
      pageSize: 10,
      total: 0,
      options: [
        {
          value: '值1',
          label: '选项1'
        },
        {
          value: '值2',
          label: '选项2'
        }
      ],
      tableData: [],
      currentPage3: 5,
      dialogVisible: false,
      labelPosition: 'right',
      formLabelAlign: {
        wxbh: void 0,
        wxbs: void 0,
        cgqbs: void 0,
        wxgzms: void 0,
        lisj: void 0,
        bcz: void 0,
        pxl: void 0,
        qj: void 0,
        sjdcj: void 0,
        jddfj: void 0,
        pjdj: void 0
      },
      taskCaseBool: 0,
      columns: [
        { label: '卫星编号', prop: 'wxbh' },
        { label: '卫星标识', prop: 'wxbs' },
        { label: '传感器标识', prop: 'cgqbs' },
        { label: '卫星工作模式', prop: 'wxgzms' },
        { label: '历元时间', prop: 'lisj' },
        { label: '半长轴', prop: 'bcz' },
        { label: '偏心率', prop: 'pxl' },
        { label: '倾角', prop: 'qj' },
        { label: '升交点赤经', prop: 'sjdcj' },
        { label: '近地点幅角', prop: 'jddfj' },
        { label: '平近点角', prop: 'pjdj' },
        { label: '操作', prop: 'action' }
      ],
      rules: {
        wxbh: [{ required: true, message: '请输入卫星编号', trigger: 'blur' }],
        wxbs: [{ required: true, message: '请输入卫星标识', trigger: 'blur' }],
        cgqbs: [
          { required: true, message: '请输入传感器标识', trigger: 'blur' }
        ],
        wxgzms: [
          { required: true, message: '请输入卫星工作模式', trigger: 'blur' }
        ],
        lisj: [{ required: true, message: '请输入历元时刻', trigger: 'blur' }],
        bcz: [{ required: true, message: '请输入半长轴', trigger: 'blur' }],
        pxl: [{ required: true, message: '请输入偏心率', trigger: 'blur' }],
        qj: [{ required: true, message: '请输入轨道倾角', trigger: 'blur' }],
        sjdcj: [
          { required: true, message: '请输入升交点赤经', trigger: 'blur' }
        ],
        jddfj: [
          { required: true, message: '请输入近地点幅角', trigger: 'blur' }
        ],
        pjdj: [{ required: true, message: '请输入平近点角', trigger: 'blur' }]
      },
      idsArr: [],
      excelFile: '',
      uploadDialog: false,
    }
  },
  computed: {},
  created() {},
  mounted() {
    this.GetObservationDataList()
  },
  methods: {
    EditObservationData(rowV) {
      this.dialogVisible = true
      // this.$nextTick(()=>{
      //   this.$refs.ruleForm.resetFields()
      // })
      this.formLabelAlign={}
      this.taskCaseBool = 0
      Object.keys(rowV).forEach(key=>{
        this.$set(this.formLabelAlign,key,rowV[key])
      })
    },
    closeModal() {
      this.$refs.obserfileRef.clearFileList()
      this.uploadDialog = false
    },
    async uploadData() {
      if (!this.excelFile) {
        this.$message({
          type: 'error',
          message: '请选择上传文件'
        })
        return false
      }
      this.$message.warning("正在导入")
      try {
        const fileData = new FormData()
        fileData.append('file', this.excelFile.raw)

        UploadObservationData(fileData).then(resposne => {
          if (resposne.data.code !== 200) {
            this.$message({
              type: 'success',
              message: '上传失败'
            })
          }
          this.uploadDialog = false
          this.pageCurrent = 1
          this.GetObservationDataList()

          this.$message({
            type: 'success',
            message: '上传成功'
          })
        })
      } catch (error) {
        this.$message.error(error)
      }
    },
    fileCallback(file) {
      this.excelFile = file
    },
    // 上传excel
    handleImportExcel() {
      this.uploadDialog = true
    },
    // 查询
    onSubmit() {
      this.pageCurrent = 1
      this.GetObservationDataList()
    },
    // 重置
    onReset() {
      this.pageCurrent = 1
      this.formInline = {}
      this.GetObservationDataList()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.GetObservationDataList()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.GetObservationDataList()
    },
    /**
     * 默认为新建任务案例
     * @param val
     */
    addNewTaskCase(val) {
      this.dialogVisible = true
      this.taskCaseBool = val
    },
    /** 保存观测卫星提交 */
    saveTaskCase(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false

        if (this.formLabelAlign.id) {
          UpdateObservationData(this.formLabelAlign).then(resposne => {
            if (resposne.data.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.pageCurrent = 1
              this.GetObservationDataList()
              this.$refs['ruleForm'].resetFields()
              this.dialogVisible = false
            } else {
              this.$message({
                type: 'error',
                message: '操作失败'
              })
            }
          })
        } else {
          SaveObservationData(this.formLabelAlign).then(resposne => {
            if (resposne.data.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.pageCurrent = 1
              this.GetObservationDataList()
              this.$refs['ruleForm'].resetFields()
              this.dialogVisible = false
            } else {
              this.$message({
                type: 'error',
                message: '操作失败'
              })
            }
          })
        }
      })
    },
    /** 获取观测卫星列表 */
    GetObservationDataList() {
      const params = {
        pageCurrent: this.pageCurrent,
        pageSize: this.pageSize,
        ...this.formInline
      }
      GetObservationDataList(params).then(resposne => {
        if (resposne.data.code == 200) {
          this.tableData = resposne.data.data.records
          this.total = resposne.data.data.total
        } else {
          this.$message({
            type: 'error',
            message: '获取失败'
          })
        }
      })
    },
    /** 取消按钮 */
    cancel() {
      this.$refs.ruleForm.clearValidate()
      this.formLabelAlign = {}
      this.dialogVisible = false
    },
    /** 删除单个卫星数据 */
    DeleteObservationData(row) {
      let param = {
        ids: []
      }
      if (row.id) {
        param.ids.push(row.id)
      } else {
        param.ids = this.idsArr.map(item => {
          return item.id
        })
      }
      if (param.ids.length == 0) {
        this.$message({
          type: 'warning',
          message: '请选择删除项'
        })
        return false
      }

      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        DeleteObservationData(param).then(resposne => {
          if (resposne.data.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.GetObservationDataList()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败'
            })
          }
        })
      })
    },
    /** 表格复选框 */
    handleSelectionChange(val) {
      this.idsArr = val
    },
    // 效验是否是数字
    handleInput(event, name) {
      if (name === 'bcz') {
        this.formLabelAlign.bcz = this.formLabelAlign.bcz.replace(
          /[^0-9.]/g,
          ''
        )
      } else if (name === 'pxl') {
        this.formLabelAlign.pxl = this.formLabelAlign.pxl.replace(
          /[^0-9.]/g,
          ''
        )
      } else if (name === 'qj') {
        this.formLabelAlign.qj = this.formLabelAlign.qj.replace(/[^0-9.]/g, '')
      } else if (name === 'sjdcj') {
        this.formLabelAlign.sjdcj = this.formLabelAlign.sjdcj.replace(
          /[^0-9.]/g,
          ''
        )
      } else if (name === 'jddfj') {
        this.formLabelAlign.jddfj = this.formLabelAlign.jddfj.replace(
          /[^0-9.]/g,
          ''
        )
      } else if (name === 'pjdj') {
        this.formLabelAlign.pjdj = this.formLabelAlign.pjdj.replace(
          /[^0-9.]/g,
          ''
        )
      }
    }
  }
}
